package com.example.administrator.express.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.administrator.express.R;
import com.example.administrator.express.http.HttpClient;
import com.example.administrator.express.http.HttpResponseHandler;
import com.example.administrator.express.model.ViewCount;
import com.example.administrator.express.model.delivery_view;
import com.example.administrator.express.model.sign_view;
import com.example.administrator.express.utils.Constant;
import com.example.administrator.express.utils.SharedPreferences;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.adapter.StaticPagerAdapter;
import com.jude.rollviewpager.hintview.ColorPointHintView;
import com.loveplusplus.update.UpdateChecker;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.FormBody;
import okhttp3.Request;

public class MainActivity extends Activity {

    @Bind(R.id.sn)
    EditText sn;
    @Bind(R.id.btnBack)
    Button btnBack;


    @Bind(R.id.searchbtn)
    ImageButton searchbtn;
    @Bind(R.id.ReceiveCount)
    RadioButton ReceiveCount;
    @Bind(R.id.DeliveryCount)
    RadioButton DeliveryCount;
    @Bind(R.id.SignCount)
    RadioButton SignCount;
    @Bind(R.id.MyNews)
    RadioButton MyNews;
    @Bind(R.id.btnreceive)
    Button btnreceive;
    @Bind(R.id.btndelivery)
    Button btndelivery;
    @Bind(R.id.btnsign)
    Button btnsign;
    private int btnid;


    private RollPagerView mRollViewPager;
    List<ViewCount> list = new ArrayList<ViewCount>();
    List<delivery_view> list1 = new ArrayList<delivery_view>();
    List<sign_view> list2 = new ArrayList<sign_view>();
    double countformonthaaa[] = new double[12];
    double countformonthaaa1[] = new double[12];
    double countformonthaaa2[] = new double[12];


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        btnBack.setVisibility(View.GONE);
        mRollViewPager = (RollPagerView) findViewById(R.id.roll_view_pager);

        //设置播放时间间隔
        mRollViewPager.setPlayDelay(2000);
        //设置透明度
        mRollViewPager.setAnimationDurtion(500);
        //设置适配器
        mRollViewPager.setAdapter(new TestNormalAdapter());

        //设置指示器（顺序依次）
        //自定义指示器图片
        //设置圆点指示器颜色
        //设置文字指示器
        //隐藏指示器
        //mRollViewPager.setHintView(new IconHintView(this, R.drawable.point_focus, R.drawable.point_normal));
        mRollViewPager.setHintView(new ColorPointHintView(this, Color.YELLOW, Color.WHITE));
        //mRollViewPager.setHintView(new TextHintView(this));
        //mRollViewPager.setHintView(null);
        UpdateChecker.checkForDialog(MainActivity.this);
        UpdateChecker.checkForNotification(MainActivity.this);




    }

    private class TestNormalAdapter extends StaticPagerAdapter {
        private int[] imgs = {
                R.drawable.main1,
                R.drawable.main2,
                R.drawable.main3,
                R.drawable.a,
        };


        @Override
        public View getView(ViewGroup container, int position) {
            ImageView view = new ImageView(container.getContext());
            view.setImageResource(imgs[position]);
            view.setScaleType(ImageView.ScaleType.CENTER_CROP);
            view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            return view;
        }


        @Override
        public int getCount() {
            return imgs.length;
        }

    }


    @OnClick({R.id.ReceiveCount, R.id.DeliveryCount, R.id.SignCount, R.id.MyNews, R.id.btnreceive, R.id.btndelivery, R.id.btnsign, R.id.searchbtn})
    public void onClick(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.ReceiveCount:
                initdata1("1");
                btnid = 4;
                break;
            case R.id.DeliveryCount:
                initdata2("2");
                btnid = 5;

                break;
            case R.id.SignCount:
                initdata3("3");
                btnid = 6;
                break;
            case R.id.MyNews:
                intent = new Intent(this, MynewsActivity.class);
                startActivity(intent);
                break;
            case R.id.btnreceive:
                btnid = 1;
                break;
            case R.id.btndelivery:
                btnid = 2;
                break;
            case R.id.btnsign:
                btnid = 3;
                break;
            case R.id.searchbtn:
                Integer snid = Integer.valueOf(sn.getText().toString());
                Intent intent1 = new Intent(this, PackageActivity.class);
                intent1.putExtra("sn", snid);
                startActivity(intent1);
                break;

        }
        if (btnid == 1 || btnid == 2 || btnid == 3)

        {

            intent = new Intent(this, CommonScanActivity.class);
            System.out.println("准备跳转");
            intent.putExtra(Constant.REQUEST_SCAN_MODE, Constant.REQUEST_SCAN_MODE_BARCODE_MODE);
            intent.putExtra("btnid", btnid);
            btnid = 0;
            System.out.println("已跳转" + btnid);
            startActivity(intent);

        } else if (btnid == 4) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    Intent intent = new Intent(MainActivity.this, LineColumnDependencyActivity.class);
                    intent.putExtra("btnid", 4);
                    intent.putExtra("receive", countformonthaaa);
                    btnid = 0;
                    startActivity(intent);
                }
            }, 1000);

        } else if (btnid == 5) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    Intent intent = new Intent(MainActivity.this, LineColumnDependencyActivity.class);
                    intent.putExtra("btnid", 5);
                    intent.putExtra("delivery", countformonthaaa1);
                    btnid = 0;
                    startActivity(intent);
                }
            }, 1000);
        } else if (btnid == 6) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {

                    Intent intent = new Intent(MainActivity.this, LineColumnDependencyActivity.class);
                    intent.putExtra("btnid", 6);
                    intent.putExtra("sign", countformonthaaa2);
                    btnid = 0;
                    startActivity(intent);
                }
            }, 1000);
        }


    }

    public void initdata1(String posttype) {
        String action = "findByLoginIdAndDate";
        SharedPreferences sharedPreferences = SharedPreferences.getInstance();
        Integer loginId = sharedPreferences.getInt("loginId", 0);

        getcountforday(posttype, action, loginId, "2016");

    }

    public void initdata2(String posttype) {
        String action = "findByLoginIdAndDate";
        SharedPreferences sharedPreferences = SharedPreferences.getInstance();
        Integer loginId = sharedPreferences.getInt("loginId", 0);

        getdeliverycountforday(posttype, action, loginId, "2016");

    }

    public void initdata3(String posttype) {
        String action = "findByLoginIdAndDate";
        SharedPreferences sharedPreferences = SharedPreferences.getInstance();
        Integer loginId = sharedPreferences.getInt("loginId", 0);

        getsigncountforday(posttype, action, loginId, "2016");

    }


    public void getsigncountforday(final String posttype, String action, int loginId, String date) {
        double[] countforday;
        try {
            String url = "http://oa24406206.carp.mopaasapp.com/";
            url = url + action;
            FormBody formBody = new FormBody.Builder()
                    .add("posttype", posttype)
                    .add("loginId", String.valueOf(loginId))
                    .add("date", date)
                    .build();

            HttpClient.post(url, formBody, new HttpResponseHandler() {
                @Override
                public void onSuccess(int statusCode, String content) {
                    System.out.println("成功:" + content);
                    Gson gson = new Gson();
                    Type listType = new TypeToken<List<sign_view>>() {
                    }.getType();

                    list2 = gson.fromJson(content, listType);


                    double countformonth[] = new double[]{0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1};

                    for (sign_view sign : list2) {
                        if (sign.getSign_time().contains("2016-01")) {
                            countformonth[0] += sign.getSign_count();
                        } else if (sign.getSign_time().contains("2016-02")) {
                            countformonth[1] += sign.getSign_count();
                        } else if (sign.getSign_time().contains("2016-03")) {
                            countformonth[2] += sign.getSign_count();
                        } else if (sign.getSign_time().contains("2016-04")) {
                            countformonth[3] += sign.getSign_count();
                        } else if (sign.getSign_time().contains("2016-05")) {
                            countformonth[4] += sign.getSign_count();
                        } else if (sign.getSign_time().contains("2016-06")) {
                            countformonth[5] += sign.getSign_count();
                        } else if (sign.getSign_time().contains("2016-07")) {
                            countformonth[6] += sign.getSign_count();
                        } else if (sign.getSign_time().contains("2016-08")) {
                            countformonth[7] += sign.getSign_count();
                        } else if (sign.getSign_time().contains("2016-09")) {
                            countformonth[8] += sign.getSign_count();
                        } else if (sign.getSign_time().contains("2016-10")) {
                            countformonth[9] += sign.getSign_count();
                        } else if (sign.getSign_time().contains("2016-11")) {
                            countformonth[10] += sign.getSign_count();
                        } else if (sign.getSign_time().contains("2016-12")) {
                            countformonth[11] += sign.getSign_count();
                        }
                    }
                    getsigncountformonth(countformonth);
//                    String s = "";
//                    for(int i=0;i<countformonth.length;i++){
//                        s = s + countformonth[i]+" ";//拼接成字符串，最终放在变量s中
//                    }
//                    SharedPreferences sharedPreferences=SharedPreferences.getInstance();
//                    sharedPreferences.putString("signcountformonth",s);

                }

                @Override
                public void onFailure(Request request, IOException e) {
                    super.onFailure(request, e);
                    System.out.println("获取失败:");
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }


    }


    //                     if(j<10) {
//                         if (viewcount.getReceive_time().contains(date[i] +"-0"+ j)) {
//                             countforday[j - 1] = viewcount.getReeceive_count();
//                         }else{
//                             countforday[j - 1]=0;
//                         }
//                     }else {
//                             if(viewcount.getReceive_time().contains(date[i]+"-"+j)){
//                                 countforday[j-1]=viewcount.getReeceive_count();
//
//                             }else{
//                                 countforday[j-1]=0;
//                             }
//                         }
//获取每日数据getcountformonth
    public void getdeliverycountforday(final String posttype, String action, int loginId, String date) {
        double[] countforday;
        try {
            String url = "http://oa24406206.carp.mopaasapp.com/";
            url = url + action;
            FormBody formBody = new FormBody.Builder()
                    .add("posttype", posttype)
                    .add("loginId", String.valueOf(loginId))
                    .add("date", date)
                    .build();

            HttpClient.post(url, formBody, new HttpResponseHandler() {
                @Override
                public void onSuccess(int statusCode, String content) {
                    System.out.println("成功:" + content);
                    Gson gson = new Gson();
                    Type listType = new TypeToken<List<delivery_view>>() {
                    }.getType();

                    list1 = gson.fromJson(content, listType);

                    double countformonth[] = new double[]{0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1};

                    for (delivery_view delivery : list1) {
                        if (delivery.getDelivery_time().contains("2016-01")) {
                            countformonth[0] += delivery.getDelivery_count();
                        } else if (delivery.getDelivery_time().contains("2016-02")) {
                            countformonth[1] += delivery.getDelivery_count();
                        } else if (delivery.getDelivery_time().contains("2016-03")) {
                            countformonth[2] += delivery.getDelivery_count();
                        } else if (delivery.getDelivery_time().contains("2016-04")) {
                            countformonth[3] += delivery.getDelivery_count();
                        } else if (delivery.getDelivery_time().contains("2016-05")) {
                            countformonth[4] += delivery.getDelivery_count();
                        } else if (delivery.getDelivery_time().contains("2016-06")) {
                            countformonth[5] += delivery.getDelivery_count();
                        } else if (delivery.getDelivery_time().contains("2016-07")) {
                            countformonth[6] += delivery.getDelivery_count();
                        } else if (delivery.getDelivery_time().contains("2016-08")) {
                            countformonth[7] += delivery.getDelivery_count();
                        } else if (delivery.getDelivery_time().contains("2016-09")) {
                            countformonth[8] += delivery.getDelivery_count();
                        } else if (delivery.getDelivery_time().contains("2016-10")) {
                            countformonth[9] += delivery.getDelivery_count();
                        } else if (delivery.getDelivery_time().contains("2016-11")) {
                            countformonth[10] += delivery.getDelivery_count();
                        } else if (delivery.getDelivery_time().contains("2016-12")) {
                            countformonth[11] += delivery.getDelivery_count();
                        }
                    }
                    getdeliverycountformonth(countformonth);
//                String s = "";
//                for(int i=0;i<countformonth.length;i++){
//                    s = s + countformonth[i]+" ";//拼接成字符串，最终放在变量s中
//                }
//                SharedPreferences sharedPreferences=SharedPreferences.getInstance();
//                sharedPreferences.putString("deliverycountformonth",s);

                }

                @Override
                public void onFailure(Request request, IOException e) {
                    super.onFailure(request, e);
                    System.out.println("获取失败:");
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }


    }


    //获取每日数据getcountformonth
    public void getcountforday(final String posttype, String action, int loginId, String date) {
        double[] countforday;
        try {
            String url = "http://oa24406206.carp.mopaasapp.com/";
            url = url + action;
            FormBody formBody = new FormBody.Builder()
                    .add("posttype", posttype)
                    .add("loginId", String.valueOf(loginId))
                    .add("date", date)
                    .build();

            HttpClient.post(url, formBody, new HttpResponseHandler() {
                @Override
                public void onSuccess(int statusCode, String content) {
                    System.out.println("成功:" + content);
                    Gson gson = new Gson();
                    Type listType = new TypeToken<List<ViewCount>>() {
                    }.getType();

                    list = gson.fromJson(content, listType);

                    double countformonth[] = new double[]{0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1};

                    for (ViewCount viewcount : list) {
                        if (viewcount.getReceive_time().contains("2016-01")) {
                            countformonth[0] += viewcount.getReeceive_count();
                        } else if (viewcount.getReceive_time().contains("2016-02")) {
                            countformonth[1] += viewcount.getReeceive_count();
                        } else if (viewcount.getReceive_time().contains("2016-03")) {
                            countformonth[2] += viewcount.getReeceive_count();
                        } else if (viewcount.getReceive_time().contains("2016-04")) {
                            countformonth[3] += viewcount.getReeceive_count();
                        } else if (viewcount.getReceive_time().contains("2016-05")) {
                            countformonth[4] += viewcount.getReeceive_count();
                        } else if (viewcount.getReceive_time().contains("2016-06")) {
                            countformonth[5] += viewcount.getReeceive_count();
                        } else if (viewcount.getReceive_time().contains("2016-07")) {
                            countformonth[6] += viewcount.getReeceive_count();
                        } else if (viewcount.getReceive_time().contains("2016-08")) {
                            countformonth[7] += viewcount.getReeceive_count();
                        } else if (viewcount.getReceive_time().contains("2016-09")) {
                            countformonth[8] += viewcount.getReeceive_count();
                        } else if (viewcount.getReceive_time().contains("2016-10")) {
                            countformonth[9] += viewcount.getReeceive_count();
                        } else if (viewcount.getReceive_time().contains("2016-11")) {
                            countformonth[10] += viewcount.getReeceive_count();
                        } else if (viewcount.getReceive_time().contains("2016-12")) {
                            countformonth[11] += viewcount.getReeceive_count();
                        }
                    }
                    getreceivecountformonth(countformonth);
//                    String s = "";
//                    for(int i=0;i<countformonth.length;i++){
//                        s = s + countformonth[i]+" ";//拼接成字符串，最终放在变量s中
//                    }
//                    SharedPreferences sharedPreferences=SharedPreferences.getInstance();
//                    sharedPreferences.putString("receivecountformonth",s);


                }

                @Override
                public void onFailure(Request request, IOException e) {
                    super.onFailure(request, e);
                    System.out.println("获取失败:");
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    public void getreceivecountformonth(double[] a) {
        this.countformonthaaa = a;

    }

    public void getdeliverycountformonth(double[] a) {
        this.countformonthaaa1 = a;

    }

    public void getsigncountformonth(double[] a) {
        this.countformonthaaa2 = a;

    }


}


